{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a7d7661f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "我在最前面，我后面是[孙悟空]：\n",
      "[w] 前进\n",
      "[s] 后退\n",
      "[q] 退出\n",
      "[d] 对话\n",
      "w\n",
      "我在最前面，我后面是[孙悟空]：\n",
      "[w] 前进\n",
      "[s] 后退\n",
      "[q] 退出\n",
      "[d] 对话\n",
      "w\n",
      "我在最前面，我后面是[孙悟空]：\n",
      "[w] 前进\n",
      "[s] 后退\n",
      "[q] 退出\n",
      "[d] 对话\n",
      "s\n",
      "我前面是[孙悟空]，我后面是[猪八戒]：\n",
      "[w] 前进\n",
      "[s] 后退\n",
      "[q] 退出\n",
      "[d] 对话\n",
      "d\n",
      "孙悟空在此！\n",
      "我前面是[孙悟空]，我后面是[猪八戒]：\n"
     ]
    }
   ],
   "source": [
    "class NPC:\n",
    "\t''' 游戏NPC '''\n",
    "\tdef __init__(self, name):\n",
    "\t\tself.name = name\n",
    "\tdef __str__(self):\n",
    "\t\t''' 为地图类where调用生成对象信息 '''\n",
    "\t\treturn '['+self.name+']'\n",
    "\t\n",
    "\tdef say(self):\n",
    "\t\treturn self.name + '在此！'\n",
    "\n",
    "class MyMap:\n",
    "\t''' 地图类 '''\n",
    "\tdef __init__(self):\n",
    "\t\t# 初始化一维地图元素\n",
    "\t\tself.line =[NPC(\"孙悟空\"), NPC(\"猪八戒\"),NPC( \"沙和尚\"), NPC(\"白龙马\"), NPC(\"妖怪婆\")]\n",
    "\t\tself.index = 0\t# 玩家位置\n",
    "\t\t\n",
    "\tdef foreward(self):\n",
    "\t\tself.index -= 1\n",
    "\t\tif self.index<=0: \n",
    "\t\t\tself.index=0\n",
    "\t\n",
    "\tdef backward(self):\n",
    "\t\tself.index += 1\n",
    "\t\tif self.index>=len(self.line): \n",
    "\t\t\tself.index=len(self.line)\n",
    "\t\n",
    "\tdef where(self):\n",
    "\t\tbefore = \"我在最前面\" if self.index<=0 else \"我前面是\"+str(self.line[self.index-1])\n",
    "\t\tafter = \"我在最后面\" if self.index>=len(self.line) else \"我后面是\"+str(self.line[self.index])\n",
    "\t\tprint(before + \"，\" + after + '：')\n",
    "\t\n",
    "\tdef dialog(self):\n",
    "\t\tface=self.line[self.index-1]\n",
    "\t\tprint(face.say())\n",
    "\t\n",
    "\n",
    "class Mud:\n",
    "\t''' 游戏交互辅助类 '''\n",
    "\tdef __init__(self):\n",
    "\t\tself.mymap = MyMap()\n",
    "\t\t\n",
    "\tdef run(self):\n",
    "\t\tmsg = \"[w] 前进\\n[s] 后退\\n[q] 退出\\n[d] 对话\\n\";\n",
    "\t\twhile True:\n",
    "\t\t\tself.mymap.where()\n",
    "\t\t\taction = input(msg)\n",
    "\t\t\tif action=='w': self.mymap.foreward()\n",
    "\t\t\telif action=='s': self.mymap.backward()\n",
    "\t\t\telif action=='d': self.mymap.dialog()                \n",
    "\t\t\telif action=='q': break\n",
    "\t\n",
    "if __name__ == '__main__':\n",
    "\tmud = Mud()\n",
    "\tmud.run()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
